package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;
import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.printeverywhere.dto.GeneralOptions;
import com.printeverywhere.util.HibernateUtil;

public class GeneralOptionsServiceDelegator {
	private static Logger log = Logger.getLogger(GeneralOptionsServiceDelegator.class);

	@SuppressWarnings("unchecked")
	public static GeneralOptions getGeneralOptions() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for GeneralOptions without search criteria".intern());
		}
		try {
			List<GeneralOptions> generalOptionsList = getList();
			return (generalOptionsList.size() > 0) ? generalOptionsList.get(0) : new GeneralOptions();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for ListingOptions without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
		}
	}

	/**
	 * Update mailing system.
	 *
	 * @param ListingOptions the mailing system
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void updateGeneralOptions(GeneralOptions generalOptions) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateGeneralOptions method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			if(generalOptions.getId().equals(null)){
				session.save(generalOptions);
			}else{
				session.update(generalOptions);
			}
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when update GeneralOptions without criteria".intern());
			if (transaction != null)
				transaction.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
	/**
	 * Gets the list of all the records from LocationCategories table without
	 * checking any predefined criteria, It simply list all the rows from
	 * database table.
	 *
	 * @return the list
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static List<GeneralOptions> getList() throws HibernateException {
		log.info(GET_LIST_WITHOUT_SEARCH_CRITERIA);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			List<GeneralOptions> list = session.createCriteria(
					GeneralOptions.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			log.error(ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			log.info(SESSION_CLOSED);
			session.flush();
			session.close();
		}
	}
	public static void main(String[] args) {
		getList();
	}

}
